<%@Page Language="VB" %>
<%@Import Namespace="System.XML" %>
<%@ Import Namespace="System.IO" %>
<html><head>
<title>Creating an XML document with an XMLTextWriter object</title>
</head>
<body bgcolor="#ffffff">
<span class="heading">Creating an XML document with an XMLTextWriter object</span><hr />
<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResults" runat="server"></div>

<script language="vb" runat="server">

Sub Page_Load(sender As Object, e As EventArgs)
   Dim strCurrentPath As String = Request.PhysicalPath
   Dim strXMLPath As String = Left(strCurrentPath, InStrRev(strCurrentPath, "\")) & "newbooklist.xml"
   Dim objXMLWriter As XmlTextWriter
   Try
      objXMLWriter = New XmlTextWriter(strXMLPath, Nothing)
      outDocURL.innerHTML = "Writing to file: <b>" & strXMLPath & "</b>"
   Catch objError As Exception
      outError.innerHTML = "<b>* Error while accessing document</b>.<br />" _
          & objError.Message & "<br />" & objError.Source
      Exit Sub  ' and stop execution
   End Try
   objXMLWriter.Formatting = Formatting.Indented
   objXMLWriter.Indentation = 3

   objXMLWriter.WriteStartDocument()

   objXMLWriter.WriteComment("Created using an XMLTextWriter - " & Now())


   objXMLWriter.WriteStartElement("BookList")
      objXMLWriter.WriteStartElement("Book")
         objXMLWriter.WriteAttributeString("Category", "Technology")
         Dim intPageCount As Integer = 1248   'numeric value to convert
         objXMLWriter.WriteAttributeString("Pagecount", intPageCount.ToString("G"))

         objXMLWriter.WriteElementString("Title", "Xây dựng và triển khai ứng dụng thương mại điện tử bằng ASP.NET (C#), C#.NET và CSDL SQL Server")
         Dim datReleaseDate As DateTime = #12/15/2003#
         objXMLWriter.WriteElementString("ReleaseDate", datReleaseDate.ToString("yyyy-MM-dd"))
         Dim intSales As Integer = 17492
         objXMLWriter.WriteElementString("Sales", intSales.ToString("G"))
         
         objXMLWriter.WriteElementString("Keyword", "Server Control, Validation Control, Html Control, DLL, DataGrid, List Grid, Web Service, ADO.NET, Mail, Upload")

         objXMLWriter.WriteStartElement("AuthorList")

            objXMLWriter.WriteElementString("Author", "Phạm Hữu Khang")
            objXMLWriter.WriteElementString("Author", "Hữu Khang")

         objXMLWriter.WriteEndElement()

      objXMLWriter.WriteEndElement()

   objXMLWriter.WriteEndElement()

   objXMLWriter.Flush()
   objXMLWriter.Close()

   Dim strXMLResult As String
   Dim objSR As StreamReader = File.OpenText(strXMLPath)
   strXMLResult = objSR.ReadToEnd()
   objSR.Close
   objSR = Nothing

   outResults.innerHTML = "<pre>" & Server.HtmlEncode(strXMLResult) & "<pre>"

End Sub
</script>
</body>
</html>
